<?php	
	// Interface function	
	function oh_module_menu($tokens) {
		global $oh_object_menu;
		if (count($tokens)>0) {
			return $oh_object_menu->menus[$tokens[0]];
		}
		return $oh_object_menu;
	} 

	// Option file required
	//require_once($oh['path']."/modules/mysql/ezsql/shared/ez_sql_core.php");
	//require_once($oh['path']."/modules/mysql/ezsql/mysql/ez_sql_mysql.php");
	
	// Main class of module
	class oh_class_menu {
		public $menus;
		function oh_class_menu() {
			$menus = array();
		}
		function define($name, $struct) {
			$this->menus[$name] = $struct;
		}
		function draw($name,$pre="<ul>",$pre_item="<li>",$post_item="</li>",$post="</ul>") {			
			$menu = $this->menus[$name];
			$this->draw_array($menu,$pre,$pre_item,$post_item,$post);
		}
		function draw_array($a,$pre="<ul>",$pre_item="<li>",$post_item="</li>",$post="</ul>") {
			if ($pre) echo $pre."\n";
			$mid = 0;
			$num = count($a);
			foreach($a as $l_item => $r_item) {
				if ($this->is_menu_item($l_item)) {
					$item = $this->parse_menu_item($l_item);
					$this->draw_menu_item($item['label'],$item['url'],$mid,$num,$pre_item,$post_item);
					if (is_array($r_item)) {
						$this->draw_array($r_item,$pre,$pre_item,$post_item,$post);
					}
				} else {
					if (is_array($r_item)) {
						
					} else if ($this->is_menu_item($r_item)) {
						$item = $this->parse_menu_item($r_item);
						$this->draw_menu_item($item['label'],$item['url'],$mid,$num,$pre_item,$post_item);
					}															
				}
				$mid++;
			}		
			if ($post) echo $post."\n";						
		}
		
		function draw_menu_item($label,$url,$mid,$n,$pre_item,$post_item) {		
			$class = array();
			if ($mid == 0) $class[] = "first";
			if ($mid == ($n-1)) $class[] = "last";
			$class = 'class="'.implode(" ",$class).'"';
						
			if ($pre_item) echo $pre_item;
			echo '<a '.$class.' href="'.$url.'">'.$label.'</a>';
			if ($post_item) echo $post_item."\n";		
		}
		
		function is_menu_item($item) {
			return !is_numeric($item);
		}
		function get_url($item) {
			$item = trim($item);
			$word = explode("=>",$item);
			return $word[1];			
		}
		function get_label($item) {
			$item = trim($item);
			$word = explode("=>",$item);
			return $word[0];			
		}
		function parse_menu_item($item) {
			$item 	= explode("=>",$item);
			$item[] = "/";
			$label 	= trim($item[0]);
			$name 	= $this->menu_name($label);
			$url	= oh("url.script").trim($item[1]);
			$url	= str_replace("%name%",$name,$url);
			$url	= str_replace("%label%",$label,$url);
			$o = array(
				"label" => $label,
				"name"	=> $name,
				"url"	=> $url,
			);
			return $o;
		}
		function menu_name($label) {
			$name = $label;
			$name = html_entity_decode($name);
			$name = strtolower($name);
			$name = preg_replace("/[^a-z0-9 ]/","",$name);
			$name = preg_replace("/[ ]+/","-",$name);
			return $name;
		}
	}
	
	// Main object of module
	global $oh_object_menu;
	$oh_object_menu = new oh_class_menu();
?>
